package com.svs.quartz.domain;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.svs.quartz.util.JdbcMapUtil;

public class SyncEntity {

	WisDataGroup wisDataGroup;
	WisDataSource wisDataSource;
	WisDataSource targetDataSource;
	JdbcTemplate jdTemplate;
	JdbcTemplate targetJdbcTemplate;
	List<WisDataCol> wisDataCols;
	WisDataCol linkCol;
	int len;

	public WisDataGroup getWisDataGroup() {
		return wisDataGroup;
	}

	public void setWisDataGroup(WisDataGroup wisDataGroup) {
		this.wisDataGroup = wisDataGroup;
	}

	public WisDataSource getWisDataSource() {
		return wisDataSource;
	}

	public void setWisDataSource(WisDataSource wisDataSource) {
		this.wisDataSource = wisDataSource;
	}

	public WisDataSource getTargetDataSource() {
		return targetDataSource;
	}

	public void setTargetDataSource(WisDataSource targetDataSource) {
		this.targetDataSource = targetDataSource;
	}

	public JdbcTemplate getJdTemplate() {
		return jdTemplate;
	}

	public void setJdTemplate(JdbcTemplate jdTemplate) {
		this.jdTemplate = jdTemplate;
	}

	public JdbcTemplate getTargetJdbcTemplate() {
		return targetJdbcTemplate;
	}

	public void setTargetJdbcTemplate(JdbcTemplate targetJdbcTemplate) {
		this.targetJdbcTemplate = targetJdbcTemplate;
	}

	public List<WisDataCol> getWisDataCols() {
		return wisDataCols;
	}

	public void setWisDataCols(List<WisDataCol> wisDataCols) {
		this.wisDataCols = wisDataCols;
	}

	public int getLen() {
		return len;
	}

	public void setLen(int len) {
		this.len = len;
	}

	public WisDataCol getLinkCol() {
		return linkCol;
	}

	public void setLinkCol(WisDataCol linkCol) {
		this.linkCol = linkCol;
	}

	public String getSelectSql() {
		String sql = " select ";
		for (int i = 0; i < len; i++) {
			WisDataCol wisDataCol = wisDataCols.get(i);
			if (i != len - 1) {
				sql += wisDataCol.getColName() + " , ";
			} else {
				sql += wisDataCol.getColName();
			}
		}
		sql += " from " + wisDataGroup.getTabName() + " where 1=1 ";

		return sql;
	}

	public String getSelectTargetSql() {
		String sql = " select ";
		for (int i = 0; i < len; i++) {
			WisDataCol wisDataCol = wisDataCols.get(i);
			if (i != len - 1) {
				sql += wisDataCol.getTargetCol() + " , ";
			} else {
				sql += wisDataCol.getTargetCol();
			}
		}
		sql += " from " + wisDataGroup.getTargetName() + " where 1=1 ";
		return sql;
	}

	public String getUpdateSql() {
		return "";
	}

	public String getUpdateTargetSql() {
		return "";
	}

	public String getInsertSql(Map<String,Object> results) {
		String sql = " insert "+wisDataGroup.getTabName() +" (";
		for (int i = 0; i < len; i++) {
			WisDataCol wisDataCol = wisDataCols.get(i);
			if (i != len - 1) {
				sql += wisDataCol.getColName() + " , ";
			} else {
				sql += wisDataCol.getColName();
			}
		}
		sql += " ) values (" ;
		for (int i = 0; i < len; i++) {
			WisDataCol wisDataCol = wisDataCols.get(i);
			Object val = JdbcMapUtil.getKey(results, wisDataCol.getTargetCol()) ;
			
			if (i != len - 1) {
				if(null == val) {
					sql += " NULL , ";
				} else {
					sql += "'"+val + "' , ";
				}
			} else {
				if(null == val) {
					sql += " NULL";
				} else {
					sql += "'"+val + "' , ";
				}
			}
		}
		sql += " ) ;" ;
		return sql ;
	}

	public String getInsertTargetSql(Map<String,Object> results) {
		String sql = " insert "+wisDataGroup.getTargetName() +" (";
		for (int i = 0; i < len; i++) {
			WisDataCol wisDataCol = wisDataCols.get(i);
			if (i != len - 1) {
				sql += wisDataCol.getTargetCol() + " , ";
			} else {
				sql += wisDataCol.getTargetCol();
			}
		}
		sql += " ) values (" ;
		for (int i = 0; i < len; i++) {
			WisDataCol wisDataCol = wisDataCols.get(i);
			Object val = JdbcMapUtil.getKey(results, wisDataCol.getColName()) ;
			if (i != len - 1) {
				if(null == val) {
					sql += " NULL , ";
				} else {
					sql += "'"+val + "' , ";
				}
			} else {
				if(null == val) {
					sql += " NULL";
				} else {
					sql += "'"+val + "' , ";
				}
			}
			
		}
		sql += " ) ;" ;
		return sql ;
	}

}
